//
//  SHPMainViewController.m
//  weibo
//
//  Created by songhuipan on 16/4/17.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

#import "SHPMainViewController.h"
#import "SHPDiscoverViewController.h"
#import "SHPHomeViewController.h"
#import "SHPProfileViewController.h"
#import "SHPMessageViewController.h"
#import "SHPTabBar.h"
#import "FCCommon.h"

@interface SHPMainViewController ()
@property (nonatomic,strong)SHPTabBar *myTabBar;
@end

@implementation SHPMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
# warning 测试发现添加控制器，并不会创建tabBar中的按钮，懒加载无处不在，viewDidLoad方法是在mainViewController初始化后执行的
    [self addChildrenViewControllers];
    
    // 测试发现此时tabBar的子控件个数为0.如果将自定义的tabBar在这里赋值，会造成，上面按钮位置布局出错
//    NSLog(@"%@",self.tabBar.subviews);
    
}
# warning 这个方法是在window.makeVisiable时执行的
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    NSLog(@"%lu",self.tabBar.subviews.count);
    
    // 将系统的tabBar更换为自定义的tabbar
    [self setValue:self.myTabBar forKey:@"tabBar"];
}
/**
 *  自定义tabBar的懒加载
 *
 *  @return 自定义tabBar
 */
- (SHPTabBar*)myTabBar {
    
    if (!_myTabBar) {
        
//        _myTabBar = [[SHPTabBar alloc]initWithFrame:CGRectMake(0, kScreenHeight -49, kScreenWidth, 49)];
        // 这里面调用init方法，也会调用SHPTabBar中的initWithFrame方法
        _myTabBar = [[SHPTabBar alloc]init];
    }
    
    return _myTabBar;
    
}
/**
 *  添加tabBarController的子控制器
 */
- (void)addChildrenViewControllers {
    
    [self makeViewController:[[SHPHomeViewController alloc]init] title:@"首页" imageName:@"tabbar_home"];
    [self makeViewController:[[SHPDiscoverViewController alloc]init] title:@"发现" imageName:@"tabbar_discover"];
    
//    [self makeViewController:[[UIViewController alloc]init] title:nil imageName:nil];
    
    [self makeViewController:[[SHPMessageViewController alloc]init] title:@"消息" imageName:@"tabbar_message_center"];
    
    [self makeViewController:[[SHPProfileViewController alloc] init] title:@"我的" imageName:@"tabbar_profile"];
    
}
/**
 *  设置tabBarController中的每个控制器
 *
 *  @param viewController 传入的每个视图控制器
 *  @param title          导航栏和tabBar的标题
 *  @param imageName      tabBar的图片名字
 */
- (void)makeViewController:(UIViewController*)viewController title:(NSString*)title imageName:(NSString*)imageName {
    
    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
    
    viewController.title = title;
    
    viewController.tabBarItem.image = [UIImage imageNamed:imageName];
    
    viewController.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]];
    
    [self addChildViewController:navigationController];
}
@end







